package com.nanning.web.controller.nanning;

import com.nanning.common.core.controller.BaseController;
import com.nanning.common.core.domain.AjaxResult;
import com.nanning.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Slf4j
@RestController
@RequestMapping("/nanning")
public class ImportController extends BaseController {

    @Autowired
    private ITaskCompleteService taskCompleteService;
    @Autowired
    private ISendCompleteService sendCompleteService;
    @Autowired
    private IIncomeCompleteService incomeCompleteService;
    @Autowired
    private IHeavyCarService heavyCarService;
    @Autowired
    private IWorkCarService workCarService;
    @Autowired
    private ITodayDataService todayDataService;
    @Autowired
    private ICarCompleteService carCompleteService;
    @Autowired
    private IImportRecordService importRecordService;

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, int type) throws Exception {
        InputStream inputStream = file.getInputStream();
        Workbook wb = WorkbookFactory.create(inputStream);
        switch (type) {
            case 0:
                Sheet lastSheet = wb.getSheetAt(wb.getNumberOfSheets() - 1);
                taskCompleteService.importData(lastSheet);
                break;
            case 1:
                Sheet sheet10 = wb.getSheetAt(0);
                sendCompleteService.importData(sheet10);
                incomeCompleteService.importData(sheet10);
                Sheet sheet2 = wb.getSheetAt(1);
                carCompleteService.importData(sheet2);
                break;
            case 2:
                Sheet sheet4 = wb.getSheetAt(3);
                heavyCarService.importData(sheet4);
                Sheet sheet6 = wb.getSheetAt(5);
                workCarService.importData(sheet6);
                break;
            case 3:
                Sheet sheet34 = wb.getSheetAt(3);
                Sheet sheet35 = wb.getSheetAt(4);
                Sheet sheet36 = wb.getSheetAt(5);
                Sheet sheet37 = wb.getSheetAt(6);
                todayDataService.importData4(sheet34);
                todayDataService.importData5(sheet35);
                todayDataService.importData6(sheet36);
                todayDataService.importData7(sheet37);
        }
        importRecordService.save(file.getOriginalFilename(), type);
        return AjaxResult.success();
    }
}
